﻿using BarcodeStandard;
using SkiaSharp;
using System;
using System.Collections.Generic;

namespace BarLib.Tools
{
    public static class BarImgage
    {
        public static Stream Make(Barcode barcode, SaveTypes type = SaveTypes.Png)
        {
            var stream = new MemoryStream();
            barcode.SaveImage(stream, type);
            return stream;
        }

        public static Barcode MakeBar(string text, BarcodeStandard.Type barType = BarcodeStandard.Type.Code128)
        {
            var bar = new Barcode(text, barType);
            bar.Height = 65;
            bar.BarWidth = 3;
            bar.IncludeLabel = true;
            bar.LabelFont = new SKFont(SKTypeface.FromFamilyName("黑体"), 21);
            bar.Alignment = AlignmentPositions.Left;
            bar.BoldText = true;
            bar.LabelAlignment = AlignmentPositions.Left;
            bar.Encode();
            return bar;
        }

        public static void StreamToFile(Stream stream, string path)
        {
            using (stream)
            {
                Console.WriteLine(stream.Length);
                stream.Seek(0, SeekOrigin.Begin);
                using (var file = File.Create(path))
                {
                    stream.CopyTo(file);
                    file.Flush();
                }
            }
        }
    }
}
